const ordersService = require("../server/orders");
const { wirteJSON } = require("../utils/func");
const path = require("path");
module.exports = class ordersController {
  // 卖家订单
  static async getSaleOrdersData(ctx) {
    try {
      const { uid, page } = ctx.request.body;
      const tableData = await ordersService.getSaleOrdersData(uid, page);
      const total = await ordersService.getSum(uid, "sale");
      wirteJSON(ctx, 200, "获取个人购物车数据成功！！", { tableData, total });
    } catch (err) {
      console.log(err);
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  // 获取卖家订单
  static async getBuyOrdersData(ctx) {
    try {
      const { buyid, page } = ctx.request.body;
      const tableData = await ordersService.getBuyOrdersData(buyid, page);
      const total = await ordersService.getSum(buyid, "buy");
      wirteJSON(ctx, 200, "获取个人购物车数据成功！！", { tableData, total });
    } catch (err) {
      console.log(err);
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  // 添加数据
  static async addOrders(ctx) {
    try {
      const { orders } = ctx.request.body;
      const data = JSON.parse(orders);
      console.log(data);
      const res = await ordersService.addOrders(data);
      if (res.affectedRows) {
        wirteJSON(ctx, 200, "数据插入成功！！", "数据插入成功！！");
      } else {
        wirteJSON(ctx, 404, "数据插入失败！！", "数据插入失败！！");
      }
    } catch (err) {
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  // 删除数据
  static async batchDeteleOrders(ctx) {
    try {
      const { orders } = ctx.request.body;
      const data = JSON.parse(orders);
      const res = await ordersService.batchDeteleOrders(data);
      if (res) {
        wirteJSON(ctx, 200, "数据删除成功！！", "数据删除成功！！");
      } else {
        wirteJSON(ctx, 404, "数据删除失败", "数据删除失败");
      }
    } catch (err) {
      wirteJSON(ctx, 500, err.message, null);
    }
  }
};
